//
//  HNSH_RechargeTopView.m
//  HNSH_SingleProject
//
//  Created by 李宇廷 on 2023/6/26.
//

#import "HNSH_RechargeTopView.h"

#import "HNSH_AuthTool.h"

@interface HNSH_RechargeTopView ()<UIImagePickerControllerDelegate, UINavigationControllerDelegate>

/* 充值金额 */
@property(nonatomic, strong) UILabel *titleLabel1;
/* ¥ */
@property (nonatomic, strong) UILabel *yangLabel;

/* lineView */
@property (nonatomic, strong) UIView *lineView;
/* 点击上传凭证 */
@property(nonatomic, strong) UILabel *titleLabel2;


@end

@implementation HNSH_RechargeTopView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        [self configUI];
    }
    return self;
}

- (void)configUI {
    [self addSubview:self.titleLabel1];
    [self addSubview:self.yangLabel];
    [self addSubview:self.textField];
    [self addSubview:self.lineView];
    
    [self addSubview:self.titleLabel2];
    [self addSubview:self.pzButton];
}

//MARK: 选择图片
- (void)chooseImage {
    UIAlertController * alertSheet = [UIAlertController alertControllerWithTitle:@"修改头像" message:@"" preferredStyle:UIAlertControllerStyleActionSheet];
    [alertSheet addAction:[UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [HNSH_AuthTool requestCameragraph:^(BOOL isAuth) {
            if (isAuth) {
                [self startChooseImage:UIImagePickerControllerSourceTypeCamera];
            } else {
                [self makeToast:@"未获得权限，无法上传图片"];
            }
        }];
    }]];
    [alertSheet addAction:[UIAlertAction actionWithTitle:@"从相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [HNSH_AuthTool requestForPhotoAuth:^(BOOL isAuth) {
            if (isAuth) {
                [self startChooseImage:UIImagePickerControllerSourceTypePhotoLibrary];
            } else {
                [self makeToast:@"未获得权限，无法上传图片"];
            }
        }];
    }]];
    [alertSheet addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
    [[UIViewControllerHelper findCurrentShowingViewController] presentViewController:alertSheet animated:YES completion:nil];
}

- (void)startChooseImage:(UIImagePickerControllerSourceType)sourceType{
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.allowsEditing = YES;
    imagePicker.delegate = self;
    imagePicker.sourceType = sourceType;
    imagePicker.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    imagePicker.modalPresentationStyle = UIModalPresentationFullScreen;
    [[UIViewControllerHelper findCurrentShowingViewController] presentViewController:imagePicker animated:YES completion:nil];
}

#pragma mark - UIImagePickerControllerDelegate Methods
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    [picker dismissViewControllerAnimated:YES completion:^{
        NSData * data = UIImageJPEGRepresentation([info objectForKey:UIImagePickerControllerEditedImage], 0.5);
        if (!data) {
            data = UIImageJPEGRepresentation([info objectForKey:UIImagePickerControllerOriginalImage], 0.5);
        }
        [self commitImgToAliyunOSS:data];
    }];
}


//MARK: 上传图片到七牛
- (void)commitImgToAliyunOSS:(NSData *)imgData{
    UIImage * image = [UIImage imageWithData:imgData];
    [ST_BaseNetManager uploadImageWithImageData:image progress:^(NSString *key, float percent) {
        
    } complate:^(NSString * _Nonnull urlString) {
        if (urlString && urlString.length) {
            self.imgString = urlString;
            [self.pzButton sd_setImageWithURL:[NSURL URLWithString:urlString] forState:UIControlStateNormal];
        }
    }];
}

- (UILabel *)titleLabel1 {
    if (!_titleLabel1) {
        _titleLabel1 = [[UILabel alloc]initWithFrame:CGRectMake(16, 16, kScreenWidth-48, 22)];
        _titleLabel1.text = @"充值金额";
        _titleLabel1.textColor = [UIColor appBlackLabelColor];
        _titleLabel1.font = [UIFont systemFontOfSize:16];
    }
    return _titleLabel1;
}

- (UILabel *)yangLabel {
    if (!_yangLabel) {
        _yangLabel = [[UILabel alloc]initWithFrame:CGRectMake(16, 50, 20, 28)];
        _yangLabel.text = @"¥";
        _yangLabel.textColor = [UIColor colorWithHexString:@"#333333"];
        _yangLabel.font = [UIFont boldSystemFontOfSize:28];
    }
    return _yangLabel;
}

- (UITextField *)textField{
    if (!_textField) {
        _textField = [[UITextField alloc]initWithFrame:CGRectMake(36, 50, kScreenWidth-68, 28)];
        _textField.textColor = [UIColor appBlackLabelColor];
        _textField.font = [UIFont boldSystemFontOfSize:20];
//        _textField.placeholder = @"请输入金额";
        NSMutableDictionary * attDic = [@{NSForegroundColorAttributeName:[UIColor colorWithHexString:@"#D3D3D3"], NSFontAttributeName:_textField.font} mutableCopy];
        NSMutableAttributedString * attPlace = [[NSMutableAttributedString alloc] initWithString:@"请输入金额" attributes:attDic];
        _textField.attributedPlaceholder = attPlace;
        _textField.keyboardType = UIKeyboardTypeDecimalPad;
    }
    return _textField;
}

- (UIView *)lineView {
    if (!_lineView) {
        _lineView = [[UIView alloc]initWithFrame:CGRectMake(16, 86, kScreenWidth-32, 1)];
        _lineView.backgroundColor = [UIColor colorWithHexString:@"#EDEDED"];
    }
    return _lineView;
}

- (UILabel *)titleLabel2 {
    if (!_titleLabel2) {
        _titleLabel2 = [[UILabel alloc]initWithFrame:CGRectMake(16, 114, kScreenWidth-48, 22)];
        _titleLabel2.text = @"点击上传凭证";
        _titleLabel2.textColor = [UIColor appBlackLabelColor];
        _titleLabel2.font = [UIFont systemFontOfSize:16];
    }
    return _titleLabel2;
}

- (UIButton *)pzButton {
    if (!_pzButton) {
        _pzButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _pzButton.frame = CGRectMake(16, 152, 77, 77);
        [_pzButton setBackgroundImage:[UIImage imageNamed:@"purse_tianjia"] forState:UIControlStateNormal];
        WeakSelf(weakSelf);
        [[_pzButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            [weakSelf chooseImage];
        }];
    }
    return _pzButton;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
